package com.forum.controller;

import com.forum.mapper.UserMessageMapper;
import com.forum.pojo.ForumArticle;
import com.forum.pojo.StudentInfo;
import com.forum.pojo.TeacherInfo;
import com.forum.result.Result;
import com.forum.service.*;
import com.forum.utils.JwtHelper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/person")
@CrossOrigin
public class PersonController {
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private TeacherInfoService teacherInfoService;
    @Autowired
    private ForumArticleService forumArticleService;
    @Autowired
    private ForumCommentService forumCommentService;
    @Autowired
    private UserMessageService userMessageService;
    @Autowired
    private RedisTemplate redisTemplate;
    //获取个人信息
    @GetMapping("/getPersonInfo")
    public Result getPersonInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        String value =(String)redisTemplate.opsForValue().get(token);
        String identity = JwtHelper.getIdentity(value);
        Long userId = JwtHelper.getUserId(value);
        switch(identity){
            case "student":
                return studentInfoService.getPersonInfo(userId);
            case "teacher":
                return teacherInfoService.getPersonInfo(userId);
            default:
                return Result.fail();
        }
    }
    //查看用户个人信息
    @GetMapping("/getPersonById")
    public Result getPersonById(@RequestParam Long userId,@RequestParam int identity){
        switch (identity){
            case 0:
                return Result.ok(studentInfoService.getPersonInfo(userId));
            case 1:
                return Result.ok(teacherInfoService.getPersonInfo(userId));
            default:
                return Result.fail();
        }
    }
    //查看他人帖子
    @ApiOperation("查看他人帖子")
    @GetMapping("/getPersonArticleById")
    public Result getPersonArticleById(@RequestParam Long userId,@RequestParam int identity){
        switch (identity){
            case 0:
                return Result.ok(studentInfoService.getPersonArticleById(userId));
            case 1:
                return Result.ok(teacherInfoService.getPersonArticleById(userId));
            default:
                return Result.fail();
        }
    }
    //修改学生个人信息
    @PostMapping("/updateStudentInfo")
    public Result updateStudentInfo(@RequestBody StudentInfo studentInfo,HttpServletRequest request){
        return Result.ok(studentInfoService.saveOrUpdate(studentInfo));
    }
    //修改老师信息
    @PostMapping("/updateTeacherInfo")
    public Result updateTeacherInfo(@RequestBody TeacherInfo teacherInfo){
        return Result.ok(teacherInfoService.saveOrUpdate(teacherInfo));
    }
    //获取个人文章
    @GetMapping("/getPersonArticle")
    public Result getPersonArticle(HttpServletRequest request){
        String token = request.getHeader("token");
        String value =(String)redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return forumArticleService.getPersonArticle(userId);
    }
    //
    @GetMapping("/getPersonComment")
    public Result getPersonComment(HttpServletRequest request){
        String token = request.getHeader("token");
        String value =(String)redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return forumCommentService.getPersonComment(userId);
    }

    /**
     * 申请添加好友
     * @param request
     * @param friendId
     * @return
     */
    @ApiOperation("申请添加好友")
    @RequestMapping(value = "/addFriend",method = RequestMethod.POST)
    public Result addFriend(HttpServletRequest request,@RequestParam Long friendId){
        String token = request.getHeader("token");
        String value =(String)redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return Result.ok(userMessageService.AddFriendRequest(userId,friendId));
    }
    @RequestMapping(value = "/judgeFriend",method =RequestMethod.GET)
    public Result judgeFriend(HttpServletRequest request,@RequestParam Long friendId){
        String token = request.getHeader("token");
        String value =(String)redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return Result.ok(userMessageService.judgeFriend(userId,friendId));
    }
    @ApiOperation("获取未读通知数，0聊天1评论2为所有点赞，4为好友申请")
    @RequestMapping(value = "/getMessageCount",method = RequestMethod.GET)
    public Result getMessageCount(HttpServletRequest request,@RequestParam Integer type){
        String token = request.getHeader("token");
        String value =(String)redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return Result.ok(userMessageService.getMessageCount(userId,type));
    }
}
